﻿Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Data
Imports System.Collections
Imports System.Windows.Forms
Imports DataObject
Imports System.Data.SqlClient

Public Class SqlServerTaiLieuDinhKem

    Private oqLBase As New hDBase
    Private oDataSet As New DataSet
    Private oDatatable As New DataTable

    Public Function GetAllTaiLieuDinhKem(ByVal sConnString As String) As DataTable
        oqLBase.Connection(sConnString)
        oDataSet = oqLBase.GetData("Get_All_TaiLieuDinhKems", "TaiLieuDinhKem")
        Return oDataSet.Tables("TaiLieuDinhKem")
    End Function

    Public Function InsertTaiLieuDinhKem(ByVal sConnString As String, ByVal oTaiLieuDinhKem As TaiLieuDinhKem) As Boolean
        Dim SqlParam As SqlParameter() = {New SqlParameter("@SoTaiLieuDinhKem", SqlDbType.Int), _
                                           New SqlParameter("@MaHopDong", SqlDbType.VarChar, 20), _
                                           New SqlParameter("@TenTaiLieuDinhKem", SqlDbType.NVarChar, 100), _
                                           New SqlParameter("@TenTepTin", SqlDbType.VarChar, 255), _
                                           New SqlParameter("@KichThuocTaiLieuDinhKem", SqlDbType.Int), _
                                           New SqlParameter("@DinhDangTepTin", SqlDbType.VarChar, 50), _
                                           New SqlParameter("@DienGiai", SqlDbType.NVarChar, 255), _
                                           New SqlParameter("@NoiDungTepTin", SqlDbType.VarBinary, Integer.MaxValue)}
        SqlParam(0).Direction = ParameterDirection.Output
        'Gán dữ liệu cho các tham số
        SqlParam(1).Value = oTaiLieuDinhKem.MaHopDong
        SqlParam(2).Value = oTaiLieuDinhKem.TenTaiLieuDinhKem
        SqlParam(3).Value = oTaiLieuDinhKem.TenTepTin
        SqlParam(4).Value = oTaiLieuDinhKem.KichThuocTaiLieuDinhKem
        SqlParam(5).Value = oTaiLieuDinhKem.DinhDangTepTin
        SqlParam(6).Value = oTaiLieuDinhKem.DienGiai
        SqlParam(7).Value = oTaiLieuDinhKem.NoiDungTepTin
        Try
            oqLBase.Connection(sConnString)
            oqLBase.RunStoreProcedures("Insert_TaiLieuDinhKem", "TaiLieuDinhKem", SqlParam)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End Try
    End Function

    Public Function UpdateTaiLieuDinhKem(ByVal sConnString As String, ByVal oTaiLieuDinhKem As TaiLieuDinhKem) As Boolean
        Dim SqlParam As SqlParameter() = {New SqlParameter("@SoTaiLieuDinhKem", SqlDbType.Int), _
                                           New SqlParameter("@MaHopDong", SqlDbType.VarChar, 20), _
                                           New SqlParameter("@TenTaiLieuDinhKem", SqlDbType.NVarChar, 100), _
                                           New SqlParameter("@TenTepTin", SqlDbType.VarChar, 255), _
                                           New SqlParameter("@KichThuocTaiLieuDinhKem", SqlDbType.Int), _
                                           New SqlParameter("@DinhDangTepTin", SqlDbType.VarChar, 50), _
                                           New SqlParameter("@DienGiai", SqlDbType.NVarChar, 255), _
                                           New SqlParameter("@NoiDungTepTin", SqlDbType.VarBinary, Integer.MaxValue)}
        SqlParam(0).Value = oTaiLieuDinhKem.SoTaiLieuDinhKem
        SqlParam(1).Value = oTaiLieuDinhKem.MaHopDong
        SqlParam(2).Value = oTaiLieuDinhKem.TenTaiLieuDinhKem
        SqlParam(3).Value = oTaiLieuDinhKem.TenTepTin
        SqlParam(4).Value = oTaiLieuDinhKem.KichThuocTaiLieuDinhKem
        SqlParam(5).Value = oTaiLieuDinhKem.DinhDangTepTin
        SqlParam(6).Value = oTaiLieuDinhKem.DienGiai
        SqlParam(7).Value = oTaiLieuDinhKem.NoiDungTepTin
        Try
            oqLBase.Connection(sConnString)
            oqLBase.RunStoreProcedures("Update_TaiLieuDinhKem", "TaiLieuDinhKem", SqlParam)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End Try
    End Function

    Public Function DeleteTaiLieuDinhKem(ByVal sConnString As String, ByVal SoTaiLieuDinhKem As Integer) As Boolean
        Dim SqlParam As SqlParameter() = {New SqlParameter("@SoTaiLieuDinhKem", SqlDbType.Int)}
        SqlParam(0).Value = SoTaiLieuDinhKem
        Try
            oqLBase.Connection(sConnString)
            oqLBase.RunStoreProcedures("Delete_TaiLieuDinhKem", "TaiLieuDinhKem", SqlParam)
            Return True
        Catch ex As SqlException
            Select Case ex.ErrorCode
                Case -2146232060
                    MessageBox.Show("Không thể xóa dữ liệu này. Nhóm vị trí này đang được sử dụng trong phần nghiệp vụ khác", "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Return False
            End Select
        End Try
    End Function

End Class
